package com.ruoyi.pingjiao.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.DataScope;

import com.ruoyi.pingjiao.domain.CourseclassStudent;
import com.ruoyi.pingjiao.domain.vo.StudentDto;
import com.ruoyi.pingjiao.domain.vo.StudentScore;
import com.ruoyi.pingjiao.mapper.CourseclassStudentMapper;
import com.ruoyi.pingjiao.service.ICourseclassStudentService;

import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 学生上课Service业务层处理
 * 
 * @author 牛亚博
 * @date 2023-07-08
 */
@Service
public class CourseclassStudentServiceImpl implements ICourseclassStudentService
{
    @Autowired
    private CourseclassStudentMapper courseclassStudentMapper;

    /**
     * 查询学生上课
     * 
     * @param studentId 学生上课主键
     * @return 学生上课
     */
    @Override
    public List<CourseclassStudent> selectCourseclassStudentByStudentId(Long studentId)
    {
        return courseclassStudentMapper.selectCourseclassStudentByStudentId(studentId);
    }

    /**
     * 查询学生上课列表
     * 
     * @param courseclassStudent 学生上课
     * @return 学生上课
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "o1")
    public List<CourseclassStudent> selectCourseclassStudentList(CourseclassStudent courseclassStudent)
    {
        return courseclassStudentMapper.selectCourseclassStudentList(courseclassStudent);
    }

    /**
     * 新增学生上课
     * 
     * @param courseclassStudent 学生上课
     * @return 结果
     */
    @Override
    public int insertCourseclassStudent(CourseclassStudent courseclassStudent)
    {
        return courseclassStudentMapper.insertCourseclassStudent(courseclassStudent);
    }

    /**
     * 修改学生上课
     * 
     * @param courseclassStudent 学生上课
     * @return 结果
     */
    @Override
    public int updateCourseclassStudent(CourseclassStudent courseclassStudent)
    {
        return courseclassStudentMapper.updateCourseclassStudent(courseclassStudent);
    }

    /**
     * 批量删除学生上课
     * 
     * @param studentIds 需要删除的学生上课主键
     * @return 结果
     */
    @Override
    public int deleteCourseclassStudentByStudentIds(Long[] studentIds)
    {
        return courseclassStudentMapper.deleteCourseclassStudentByStudentIds(studentIds);
    }

    /**
     * 删除学生上课信息
     * 
     * @param studentId 学生上课主键
     * @return 结果
     */
    @Override
    public int deleteCourseclassStudentByStudentId(Long studentId)
    {
        return courseclassStudentMapper.deleteCourseclassStudentByStudentId(studentId);
    }

    @Override
    public List<CourseclassStudent> selectCourseByStudentId(StudentDto studentVo) {
        return courseclassStudentMapper.selectCourseByStudentId(studentVo);
    }

    @Override
    public List<String> selectTermByStudentId(Long userId) {
        List<String> list = courseclassStudentMapper.selectTermByStudentId(userId);
        JSONArray jsonArray=new JSONArray();

        for (String s:list
             ) {

            JSONObject jsonObject=new JSONObject();
            jsonObject.put("name",s);

            jsonArray.add(jsonObject);
        }

        System.out.println(list);
        return Collections.arrayToList(jsonArray.toArray());
    }

    @Override
    public List<CourseclassStudent> selectScoreByStudentId(StudentDto studentVo) {
        return courseclassStudentMapper.selectScoreByStudentId(studentVo);
    }

    @Override
    public List<StudentScore> exportScore(Long courseclassId) {
        List<CourseclassStudent> list = courseclassStudentMapper.exportScore(courseclassId);
        List<StudentScore>studentScores=new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList<>()).stream()
                .filter(item->item!=null).forEach(item->{
                    StudentScore studentScore=new StudentScore();
                    StudentScore studentScore1= courseclassStudentMapper.selectSocre(item.getCourseclassId());
                    studentScore.setCourseName(item.getCourse().getCourseName());
                    studentScore.setMetaclassName(item.getCourseclassMetaclass().getMetaclass().getMetaclassName());
                    studentScore.setRealName(item.getCourseclassTeacher().getTeacher().getRealName());
                    studentScore.setTopScore(studentScore1.getTopScore());
                    studentScore.setLowScore(studentScore1.getLowScore());
                    studentScore.setAverageScore(studentScore1.getAverageScore());
                    studentScore.setNumber1(studentScore1.getNumber1());
                    studentScore.setNumber2(studentScore1.getNumber2());
                    studentScore.setNumber3(studentScore1.getNumber3());
                    studentScore.setNumber4(studentScore1.getNumber4());
                    studentScore.setNumber5(studentScore1.getNumber5());
                    studentScores.add(studentScore);
                });
        return studentScores;
    }
}
